import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userName:sessionStorage.getItem('username')|| false,
    isLogin:sessionStorage.getItem('isLogin') || false,
    userData:JSON.parse(sessionStorage.getItem('userData')) || [],
    cartCount:JSON.parse(sessionStorage.getItem('cartCount')) || 0
  },
  getters:{
    userName:state => state.userName,
    isLogin:state => state.isLogin,
    userData:state => state.userData,
    cartCount:state  => state.cartCount

  },
  mutations: {
    changeLogin(state,is){
      sessionStorage.setItem('isLogin',is);
      state.isLogin = is
    },
    saveCartCount(state, carCount) {
      state.cartCount = carCount;
    },
    saveUserData(state,userData){
      sessionStorage.setItem('userData',userData)
      state.userData = userData;
    }
  },
  actions: {
    saveCartCount(context, count) {
      context.commit('saveCartCount', count);
    },
    saveUserData(context,userdata){
       context.commit('saveUserData',userdata)
    }
  },
  modules: {
  }
})
